package org.tiger.common.util;

import java.util.Calendar;
import java.util.Date;


public class DateUtil {

	public final static String format1 = "yyyy-MM-dd";
	
	
	
	public static void main(String[] args) {
		Date date =geLastWeekMonday(new Date(),1); 
		Date date1 =geLastWeekMonday(new Date(),0); 
		System.out.println(date);
		System.out.println(date1);
		
		Date date2 =getmon(-1,1); 
		Date date3 =getmon(-1,0); 
		System.out.println(date2);
		System.out.println(date3);
		
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		// 获得当前日期是一个星期的第几天
		int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
		int dayMonth = cal.get(Calendar.DAY_OF_MONTH);
			Calendar cal1 = Calendar.getInstance();
		cal1.setTime(new Date());
		cal1.add(Calendar.SECOND, 3600);
		System.out.println(cal1.getTime()+"   dayWeek");
		System.out.println(dayWeek+"   dayWeek");
		System.out.println(dayMonth+"   dayMonth");
	}
	
	public static Date getThisWeekMonday(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		// 获得当前日期是一个星期的第几天
		int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
		if (1 == dayWeek) {
			cal.add(Calendar.DAY_OF_MONTH, -1);
		}
		// 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
		cal.setFirstDayOfWeek(Calendar.MONDAY);
		// 获得当前日期是一个星期的第几天
		int day = cal.get(Calendar.DAY_OF_WEEK);
		// 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
		cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
		return cal.getTime();
	}
	
	public static Date geLastWeekMonday(Date date,int unit) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(getThisWeekMonday(date));
		cal.add(Calendar.DATE, unit);
		return cal.getTime();
	}
	
	public static Date getmon(int amount ,int unit) {
		Calendar cale = Calendar.getInstance();
	        cale.add(Calendar.MONTH, amount);
	        cale.set(Calendar.DAY_OF_MONTH, unit);
	        return cale.getTime();
	}

	
}
